package com.ygqh.baby.service.impl;

import java.util.Date;
import java.util.List;

import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.ao.WholeSaleRecordType;
import com.ygqh.baby.po.YgWholesalerPrebillRecordDetail;
import com.ygqh.baby.mapper.YgWholesalerPrebillRecordDetailMapper;
import com.ygqh.baby.service.YgWholesalerPrebillRecordDetailService;
import com.ygqh.baby.template.AbstractSplitListTemplate;
import com.ygqh.baby.utils.DateConvertUtils;
import org.springframework.util.Assert;

import java.util.Arrays;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.ygqh.baby.po.YgWholesalerPrebillRecordDetailExample;
import com.ygqh.baby.ao.DataStatus;

/**
 * @author Ss
 */
@Service
public class YgWholesalerPrebillRecordDetailServiceImpl implements YgWholesalerPrebillRecordDetailService {

	@Autowired
	private YgWholesalerPrebillRecordDetailMapper ygWholesalerPrebillRecordDetailMapper;

	@Override
	public List<YgWholesalerPrebillRecordDetail> find() {
		YgWholesalerPrebillRecordDetailExample example = new YgWholesalerPrebillRecordDetailExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygWholesalerPrebillRecordDetailMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgWholesalerPrebillRecordDetailExample example = new YgWholesalerPrebillRecordDetailExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygWholesalerPrebillRecordDetailMapper.countByExample(example);
	}

	@Override
	public YgWholesalerPrebillRecordDetail findById(Long id) {
		return ygWholesalerPrebillRecordDetailMapper.selectByPrimaryKey(id);
	}


	@Override
	public int save(YgWholesalerPrebillRecordDetail ygWholesalerPrebillRecordDetail) {
		return ygWholesalerPrebillRecordDetailMapper.insertSelective(ygWholesalerPrebillRecordDetail);
	}

	@Override
	public int update(YgWholesalerPrebillRecordDetail ygWholesalerPrebillRecordDetail) {
		return ygWholesalerPrebillRecordDetailMapper.updateByPrimaryKeySelective(ygWholesalerPrebillRecordDetail);
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgWholesalerPrebillRecordDetailExample example = new YgWholesalerPrebillRecordDetailExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgWholesalerPrebillRecordDetail record = new YgWholesalerPrebillRecordDetail();
		record.setStatus(DataStatus.Delete);

		return ygWholesalerPrebillRecordDetailMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(YgWholesalerPrebillRecordDetail ygWholesalerPrebillRecordDetail) {
		int r;
		if (ygWholesalerPrebillRecordDetail.getId() != null) {
			r = this.update(ygWholesalerPrebillRecordDetail);
		} else {
			r = this.save(ygWholesalerPrebillRecordDetail);
		}
		return r;
	}

	@Override
	public ResultSet<YgWholesalerPrebillRecordDetail> searchDetail(QueryInfo queryInfo, String wholesalerName, String detailCode, String prebillCode,
																   Date startDate, Date endDate, WholeSaleRecordType recordType) {
		if (startDate != null) {
			startDate = DateConvertUtils.getDateStart(startDate);
		}
		if (endDate != null) {
			endDate = DateConvertUtils.addDay(DateConvertUtils.getDateStart(endDate), 1);
		}
		int count = ygWholesalerPrebillRecordDetailMapper.countSuper(wholesalerName, detailCode, prebillCode, startDate, endDate, recordType.name());
		if (count == 0) {
			return new ResultSet<>();
		}
		List<YgWholesalerPrebillRecordDetail> list = ygWholesalerPrebillRecordDetailMapper
				.selectSuper(queryInfo, wholesalerName, detailCode, prebillCode, startDate, endDate, recordType.name());
		return new ResultSet<>(count, list);
	}

	@Override
	public void saveBatch(List<YgWholesalerPrebillRecordDetail> prebillRecordDetailList) {
		new AbstractSplitListTemplate<YgWholesalerPrebillRecordDetail>(prebillRecordDetailList) {
			@Override
			public int execute(List<YgWholesalerPrebillRecordDetail> recordList) {
				return ygWholesalerPrebillRecordDetailMapper.insertBatch(recordList);
			}
		}.split();
	}

	@Override
	public List<YgWholesalerPrebillRecordDetail> findByPrebillCode(String prebillCode) {
		return ygWholesalerPrebillRecordDetailMapper.selectByPrebillCode(prebillCode);
	}

}

